package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.service.db.YxyKeChengService;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxylessonsTopicDelItemController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsTopicLessonRepository lessonRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyKeChengService yxyKeChengServiceImpl;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/item/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String itemId = reqModel.itemId;
        YxyLessonsTopicLessonItem item = itemRepository.findById(itemId);
        if (null != item) {
            item.setStatus(-1);
            itemRepository.saveAndFlush(item);

            Integer duration = 0;
            YxyLessonsVideo video = videoRepository.findById(item.getVideoId());
            if (video != null) {
                duration = video.getDuration();
            }

            String lessonId = item.getLessonId();
            YxyLessonsTopicLesson lesson = lessonRepository.findById(lessonId);
            if (null != lesson) {
                lesson.setSync(0);
                Integer count = (lesson.getCount() > 0) ? lesson.getCount() - 1 : 0;
                lesson.setCount(count);
                lesson.setIsVisible(0);
                lesson.setDownType("其它");
                lesson.setDuration((lesson.getDuration() - duration > 0) ? lesson.getDuration() - duration : 0);
                lessonRepository.saveAndFlush(lesson);
            }

            // 每次添加课时修改课时的排序
            List<Map<String, Object>> itemList = yxyKeChengServiceImpl.findTopicItemList(lessonId);

            Integer orderNum = 1;
            for (Map<String, Object> map : itemList) {
                itemId = map.get("id") + "";
                YxyLessonsTopicLessonItem it = itemRepository.findById(itemId);
                it.setOrderNum(orderNum);
                itemRepository.saveAndFlush(it);
                orderNum++;
            }
        }
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String itemId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(itemId), "课时不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer itemLength;
    }
}
